;
(function () {

  'use strict';

  angular.module('services.customActivity.summary')
    .service('summaryProvinceAssessService', summaryProvinceAssessService);

  function summaryProvinceAssessService(Restangular, generatorSummaryPageService, COREDATA) {

    var that = this;

    var option = {
      title: {
        text: '统一支付',
        x: 'center'
      },
      tooltip: {
        trigger: 'yxis'
      },
      legend: {
        data: []
      },
      toolbox: {},
      xAxis: [
        {
          type: 'category',
          data: []
        }
      ],
      yAxis: [
        {
          type: 'value',
          name: '平均得分',
          max: 1,
          min: 0,
          axisLabel: {show: true, interval: 0, textStyle: {baseline: "middle"}}
        }
      ],
      series: null,
      grid: {
        y: '20%',
        height: '62%'
      }
    };

    this.queryBySys = function (sys, element) {

      var sysName = '';

      if (generatorSummaryPageService.pageInfo.preview) {
        setTimeout(function () {
          that.loading = true;
          switch (sys) {
            case 'UPAY':
              sysName = '统一支付';
              break;
            case 'TMALL':
              sysName = '天猫旗舰店';
              break;
            case 'SCPAY':
              sysName = '移动商城';
              break;
            case 'BANK':
              sysName = '银行总对总';
              break;
            case 'VCES':
              sysName = '有价卡';
              break;
            case 'LINGXI':
              sysName = '灵犀语音';
              break;
          }

          if (sys === 'BANK') {
            option.series = [
              {
                name: '缴费交易得分',
                type: 'bar',
                data: []
              },
              {
                name: '签约交易得分',
                type: 'bar',
                data: []
              }
            ];
          } else {
            option.series = [
              {
                name: '得分',
                type: 'bar',
                data: []
              }
            ];
          }
          option.title.text = sysName;
          option.xAxis[0].data = [];
          angular.forEach(generatorSummaryPageService.pageInfo.provinceCodes, function (code) {
            angular.forEach(COREDATA.PROVINCES, function (province) {
              if (code === province.provinceCode) {
                option.series[0].data.push(0);
                option.series[1] && option.series[1].data.push(0);
                option.xAxis[0].data.push(province.provinceName);
              }
            });
          });
          (echarts.init(element)).setOption(option);
        }, 0);
        return;
      }

      that.loading = false;
      Restangular.all('sas/customActivity/summary/provinceAssess/query').post({
        activityId: generatorSummaryPageService.pageInfo.activityId
      }).then(function (result) {
        that.loading = true;
        if (result.state === '10000') {
          switch (sys) {
            case 'UPAY':
              sysName = '统一支付';
              break;
            case 'TMALL':
              sysName = '天猫旗舰店';
              break;
            case 'SCPAY':
              sysName = '移动商城';
              break;
            case 'BANK':
              sysName = '银行总对总';
              break;
            case 'VCES':
              sysName = '有价卡';
              break;
            case 'LINGXI':
              sysName = '灵犀语音';
              break;
          }

          if (sys === 'BANK') {
            option.series = [
              {
                name: '缴费交易得分',
                type: 'bar',
                data: []
              },
              {
                name: '签约交易得分',
                type: 'bar',
                data: []
              }
            ];
          } else {
            option.series = [
              {
                name: '得分',
                type: 'bar',
                data: []
              }
            ];
          }
          option.title.text = sysName;
          option.xAxis[0].data = [];
          angular.forEach(result.data[sys.toLowerCase()], function (obj) {
            option.series[0].data.push(obj.point);
            option.series[1] && option.series[1].data.push(obj.signPoint);
            option.xAxis[0].data.push(obj.provinceName);
          });
        }

        (echarts.init(element)).setOption(option);
      });
    };
  }

  summaryProvinceAssessService.$inject = ['Restangular', 'generatorSummaryPageService', 'COREDATA'];
})();